﻿#region Librerias

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SqlServer.Server;
using System.Security.Cryptography;
using System.Data.SqlTypes;

#endregion


namespace ESF.MSSQLHelper
{
	public static class TextHelper
	{
		[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = true, IsPrecise = true)]
		public static SqlString GetMD5FromString (SqlString pWord)
		{
			if (string.IsNullOrEmpty (pWord.Value))
			{
				return new SqlString ();
			}

			return new SqlString (GetMD5HashBytes (System.Text.Encoding.Default.GetBytes(pWord.Value)));
		}


		private static string GetMD5HashBytes(byte[] pData)
		{
			using (MD5 md5 = MD5.Create())
			{
				return BitConverter.ToString(md5.ComputeHash(pData));
			}
		}
	}
}
